
#include <stdio.h>
#include <stdlib.h>
 
size_t mygetline(char **ptr,size_t *n,FILE *fp)
{
	int ch;
	size_t i = 0;
	if(*n == 0)
	{
		*n = 120;
	}
	*ptr = malloc(*n);
	if(*ptr == NULL)
		return -1;
	
	while((ch = fgetc(fp)) != '\n')  
	{
		if(ch == EOF)
			return -1;
		if(i+2 >= *n)	
		{
			*n += 120;
		      	*ptr = realloc(*ptr,*n);
		}
		(*ptr)[i++] = ch;
	}
	(*ptr)[i++] = '\0';
	return i;
}
 
int main(int argc,char * argv[])
{
	FILE *fp = NULL;
	char *p = NULL;
	size_t size = 10;
	size_t n;
 
	if(argc < 2)
	{
		fprintf(stderr,"Usage...\n");
		exit(1);
	}
 
	fp = fopen(argv[1],"r");
	if(fp == NULL)
	{
		perror("fopen error");
		exit(1);
	}
	
 
	while((n = mygetline(&p,&size,fp)) >= 0)
	{
		printf("size of line = %ld,size = %ld\n",n,size);
		puts(p);
	}
	
	free(p);
	fclose(fp);
	return 0;
}